/**
 * @file     summaryRanges.ts
 * @brief    [228. 汇总区间](https://leetcode.cn/problems/summary-ranges/description/)
 * @author   Zhu
 * @date     2022-11-06 01:14
 */

function summaryRanges(nums: number[]): string[] {
    const res: string[] = [];
    if (nums.length < 2) return nums.map(String);

    let start = nums[0];
    const pre = nums.reduce((pre: number, cur: number) => {
        if (pre + 1 !== cur) {
            if (pre === start) res.push(start + '');
            else res.push(start + '->' + pre);
            start = cur;
        }

        return cur;
    });

    if (pre === start) res.push(start + '');
    else res.push(start + '->' + pre);

    return res;
}
